﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ETWJ.DTO;
using ETWJ.Iservice;
using ETWJ.Model;
using ETWJ.Service;
using Microsoft.AspNetCore.Mvc;

namespace ETWJ.ERP.Controllers
{
    public class ProductController : Controller
    {

        public IProductService<Product, ProductDTO> productService { get; set; }
        public ProductController(IProductService<Product, ProductDTO> productService)
        {
            this.productService = productService;
        }


        public async Task<IActionResult> BatchDeleted(string ids)
        {
            string[] empIds = ids.Split(',');
            List<int> deletedResult = new List<int>();
            List<int> deletedResultIds = new List<int>();
            foreach (var id in empIds)
            {
                var data = await productService.GetByIdAsync(Convert.ToInt32(id));
                if (data != null)
                {
                    var result = await productService.MarkDeleted(data.Id);
                    deletedResult.Add(result);
                    deletedResultIds.Add(data.Id);
                }
            }

            if (deletedResult.Contains(-1))
            {
                string deletedFaillids = "";
                foreach (var id in deletedResultIds)
                {
                    deletedFaillids += id + "-";
                }
                return RedirectToAction("Tip", "Home", new { redirectUrl = "/Product/List", msg = "删除失败！删除失败的编号为：" + deletedFaillids, isSuccess = "OK" });
            }

            return RedirectToAction("Tip", "Home", new { redirectUrl = "/Product/List", msg = "删除成功！", isSuccess = "OK" });

        }

        [HttpPost]
        public async Task<IActionResult> Edit(ProductDTO dto)
        {
            var model=await productService.GetByIdAsync(dto.Id);
            if(model==null)
            {
                return RedirectToAction("Tip", "Home", new { redirectUrl = "/Product/List", msg = "不存在该产品！", isSuccess = "error" });
            }

            var result=await productService.UpdateAsync(dto);
            if (result <= 0)
            {
                return RedirectToAction("Tip", "Home", new { redirectUrl = "/Product/List", msg = "修改失败！", isSuccess = "error" });

            }
            return RedirectToAction("Tip", "Home", new { redirectUrl = "/Product/List", msg = "修改成功！", isSuccess = "ok" });
        }

        public async Task<IActionResult> Edit(int id)
        {

            var model=await productService.GetByIdAsync(id);
            if (model == null)
            {
                return RedirectToAction("Tip", "Home", new { redirectUrl = "/Product/List", msg = "不存在该产品！", isSuccess = "error" });
            }
            return View(model);
        }

        [HttpPost]
        public async Task<IActionResult> Add(Product product)
        {
            var resp=await productService.GetByIdAsync(product.Id);
            if (resp != null)
            {
                return RedirectToAction("Tip", "Home", new { redirectUrl = "/Product/List", msg = "已经存在该产品！", isSuccess = "error" });
            }

            var result =await productService.AddNewAsync(product);
            if (result <= 0)
            {
                return RedirectToAction("Tip", "Home", new { redirectUrl = "/Product/List", msg = "添加失败！", isSuccess = "error" });

            }
            return RedirectToAction("Tip", "Home", new { redirectUrl = "/Product/List", msg = "添加成功！", isSuccess = "ok" });
        }

        public async Task<IActionResult> Add()
        {
            return View();
        }

        public async Task<IActionResult> List(int pageIndex=1,int count=5)
        {
            if (pageIndex <= 0)
            {
                pageIndex = 1;
            }



            var model = await productService.GetByPage(pageIndex, 5);

            var totalCount = await productService.GetTotalCountAsync();
            var currentPageIndex = pageIndex;

            var pageCount = Math.Ceiling(totalCount / 5.0);

            ViewBag.PageCount = pageCount;
            ViewBag.CurrentPageIndex = currentPageIndex;

            return View(model);
        }

        public IActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> GetProductInfoById(int id)
        {
            var model=await productService.GetByIdAsync(id);
            if (model== null)
            {
                return RedirectToAction("Tip", "Home", new { redirectUrl = "/Product/List", msg = "不存在该产品！", isSuccess = "error" });
            }
            return Json(new { data = model, msg = "获取成功" });
            //return Json(new { Id = 1, data = 2, Name = "yjc" });
        }
    }
}